package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jcx.ldzj.entity.SysMenu;
import com.jcx.ldzj.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ld
 * @since 2021-06-25
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys-menu")
@Api(value = "菜单接口", tags = "菜单接口")
public class SysMenuController extends ApiController {
        @Autowired
        private ISysMenuService iSysMenuService;
       /**
       *
       * @param id
       * @return
       */
       @GetMapping("/{id}")
       @RequiresPermissions("basic:sysmenu:view")
       @ApiOperation(value = "详情", notes = "传入id")
       public R<SysMenu> view(@PathVariable("id") String id) {
          SysMenu user = iSysMenuService.getById(id);
          return R.ok(user);
        }
        /**
        * 新增
        * @param sysMenu
        * @return
        */
        @PostMapping
        @RequiresPermissions("basic:sysmenu:add")
        @ApiOperation(value = "新增", notes = "传入SysMenu")
        public R add(@Valid @RequestBody SysMenu sysMenu) {
          return this.success(iSysMenuService.saveMenu(sysMenu));
        }


        /**
        * 修改
        * @param sysMenu
        * @return
        */
        @PutMapping
        @RequiresPermissions("basic:sysmenu:update")
        @ApiOperation(value = "修改", notes = "传入SysMenu")
        public R edit(@Valid @RequestBody SysMenu sysMenu) {
          return this.success(iSysMenuService.updateMenuById(sysMenu));
        }

        /**
        * 删除
        */
        @DeleteMapping("/{menuId}")
        @RequiresPermissions("basic:sysmenu:delete")
        @ApiOperation(value = "删除", notes = "传入ids")
        public R del(@ApiParam(value = "主键集合", required = true) @PathVariable("menuId") String menuId) {
          return this.success(iSysMenuService.removeById(menuId));
        }


        /**
         *  查询树结构的菜单，用于菜单页面的列表查询
         * @return
         */
        @GetMapping("/getMenuTree")
        @RequiresPermissions("basic:sysmenu:menuTree")
        @ApiOperation(value = "查询树结构的菜单", notes = "")
        public R<List<SysMenu>> getMenuTree() {
            return R.ok(iSysMenuService.getMenuTreeAll());
        }


    /**
     * 左侧菜单树的列表
     * @return
     */
    @GetMapping("/getMenuTreeByAuth")
    @ApiOperation(value = "查询树结构的菜单", notes = "")
    public R<List<SysMenu>> getMenuTreeByAuth() {
        return iSysMenuService.getMenuTree();
    }
 }
